package com.gmstu.springframework.mvc.servlet;

import java.io.File;
import java.util.Locale;

/**
 * @author John.zhang
 * @version Id: GpViewResolver, v 1.0 2020/8/24 15:19 ZhiYong Exp $
 */
public class GpViewResolver {

    private final String DEFAULT_TEMPLATE_SUFFIX = ".html";

    private File templateRootDir;

    public GpViewResolver(String templateRoot) {
        String templateRootPath = this.getClass().getClassLoader().getResource(templateRoot).getFile();
        templateRootDir = new File(templateRootPath);
    }

    public GpView resolveViewName(String viewName, Locale locale) {
        if (null == viewName || "".equals(viewName.trim())) {
            return null;
        }
        viewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFIX) ? viewName : viewName + DEFAULT_TEMPLATE_SUFFIX;
        File templateFile = new File((templateRootDir.getPath() + "/" + viewName).replace("/+", "/"));
        return new GpView(templateFile);
    }
}
